12. Enable Location Tracking
L4 A11 Enable Location Tracking
Reference Documentation
Users often use Google Maps to see their current location. To display the device location on your map, you can use the location-data layer.
The location-data layer adds a My Location button to the top-right side of the map. When the user taps the button, the map centers on the device's location. The location is shown as a blue dot if the device is stationary, and as a blue chevron if the device is moving.
In this task, you enable the location-data layer.
Enabling location tracking in Google Maps requires a single line of code. However, you must make sure that the user has granted location permissions (using the runtime-permission model).
In this step, you request location permissions and enable the location tracking.
- In the
AndroidManifest.xmlfile, verify that theFINE_LOCATIONpermission is already present. Android Studio inserted this permission when you selected the Google Maps template.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- In
MapsActivity.kt, create aREQUEST_LOCATION_PERMISSIONvariable.
private val REQUEST_LOCATION_PERMISSION = 1
- To check if permissions are granted, create a method in the
MapsActivity.ktcalledisPermissionGranted(). In this method, check if the user has granted the permission.
private fun isPermissionGranted() : Boolean {
return ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION) === PackageManager.PERMISSION_GRANTED
}
- To enable location tracking in your app, create a method in the
MapsActivity.ktcalledenableMyLocation()that takes no arguments and doesn't return anything.
Check for theACCESS_FINE_LOCATIONpermission. If the permission is granted, enable the location layer. Otherwise, request the permission:
private fun enableMyLocation() {
if (isPermissionGranted()) {
map.setMyLocationEnabled(true)
}
else {
ActivityCompat.requestPermissions(
this,
arrayOf<String>(Manifest.permission.ACCESS_FINE_LOCATION),
REQUEST_LOCATION_PERMISSION
)
}
}
- Call
enableMyLocation()from theonMapReady()callback to enable the location layer.
override fun onMapReady(googleMap: GoogleMap) {
…
enableMyLocation()
}
- Override the
onRequestPermissionsResult()method. If therequestCodeis equal toREQUEST_LOCATION_PERMISSIONpermission is granted, and if thegrantResultsarray is non empty withPackageManager.PERMISSION_GRANTEDin its first slot, callenableMyLocation():
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray) {
// Check if location permissions are granted and if so enable the
// location data layer.
if (requestCode == REQUEST_LOCATION_PERMISSION) {
if (grantResults.size > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
enableMyLocation()
}
}
}
- Run the app. There should be a pop up requesting access to the device's location. Allow the permission.
Note that this pop up may look different if you are not running the recommended API 29.
The map will now display the device's current location using a blue dot. Notice that there is a location button on the top right. If you move the map away from your location and click on this button it will center the map back to the device location.